package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {

    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    // 查询通用设置概要
    public ResponseEntity settings() {
      //1. 获取线程内userId
        User user = UserHolder.get();
        //2. 根据userId查询陌生人问题
        Question question = questionService.findByUserId(user.getId());
        //3. 根据userId查询推送通知
        Notification notification = notificationService.findByUserId(user.getId());
        //4. 封装vo
        SettingVo vo = new SettingVo();
        vo.setId(user.getId()); //用户id
        if(question != null){
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if(notification != null){ //推送通知
            vo.setGonggaoNotification(notification.getGonggaoNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
            vo.setLikeNotification(notification.getLikeNotification());
        }
        vo.setPhone(user.getMobile());//手机号
        //5. 返回vo
        return ResponseEntity.ok(vo);
    }

    // 陌生人设置问题
    public void setQuestion(String content) {
      //1. 获取线程userId
        Long userId = UserHolder.get().getId();
        //2. 根据userId查询rpc的陌生人问题
        Question question = questionService.findByUserId(userId);
        //3. 判断
        if(question == null){ //没有设置过
            //封装
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            //新增
            questionService.save(question);
        }else { //设置过
            question.setStrangerQuestion(content);
            //更新
            questionService.update(question);
        }
    }

    // 设置推送通知
    public void setNotification(Notification notificationParam) {
        // 1. 获取线程内userId
        Long userId = UserHolder.get().getId();
        //2. 根据userId查询rpc
        Notification notification = notificationService.findByUserId(userId);
        //3. 判断
        if(notification == null){ //没查到
            //封装
            notificationParam.setUserId(userId);
            // 新增
            notificationService.save(notificationParam);
        }else { //查到了
            //封装
            notificationParam.setId(notification.getId());
            //更新
            notificationService.update(notificationParam);

        }
    }

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    // 黑名单分页查询
    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {
        //1. 获取线程userId
        Long userId = UserHolder.get().getId();
        //2. 分页查询黑名单
        PageBeanVo pageBeanVo = blackListService.findByPage(userId, pageNum, pageSize);
        //3. 根据黑名单id查询userIno

        //3-1 获取黑名单集合
        List<BlackList> blackLists = (List<BlackList>) pageBeanVo.getItems();
        // 3-2 声明userInfo集合
        ArrayList<UserInfo> userInfoList = new ArrayList<>();
        //3-3 遍历黑名单
        if (CollectionUtil.isNotEmpty(blackLists)) {
            for (BlackList blackList : blackLists) {
                // 取出黑名单id
                Long blackUserId = blackList.getBlackUserId();
                // 查询userInfo
                UserInfo blackUserInfo = userInfoService.findById(blackUserId);
                // 添加集合
                userInfoList.add(blackUserInfo);
            }
        }
        //4. 将userInfo设置到pageBeanVo
        pageBeanVo.setItems(userInfoList);
        //5. 返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    // 删除黑名单
    public void deleteBlackList(Long blackUserId) {
        //1. 获取线程内userId
        Long userId = UserHolder.get().getId();
        //2. 调用rpc删除黑名单
        blackListService.delete(userId,blackUserId);
    }
}
